package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.StaffStationChange;
import com.xmy.cultivate.entity.views.PersonManage;
import com.xmy.cultivate.mapper.StaffStationChangeMapper;
import com.xmy.cultivate.mapper.StationMapper;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.service.IStaffStationChangeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 岗位调动 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-02-01
 */
@Service
public class StaffStationChangeServiceImpl extends ServiceImpl<StaffStationChangeMapper, StaffStationChange> implements IStaffStationChangeService {

    @Autowired
    StationMapper stationMapper;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IDictService iDictService;

    @Autowired
    StaffStationChangeMapper staffStationChangeMapper;

    @Override
    @Transactional
    public Result stationChange(PersonManage personManage){
        for (StaffStationChange staffStationChange:personManage.getStaffStationChangeList()){
            Staff staff = iStaffService.getById(staffStationChange.getStaffId());
            if (staff == null){
                continue;
            }
            staff.setSuperiorId(staffStationChange.getSuperiorId());
            staff.setStationId(staffStationChange.getStationId());
            String stationName = stationMapper.getStationNameForId(staffStationChange.getStationId());
            if (staffStationChange.getOldStationId() != null) {
                String oldStationName = stationMapper.getStationNameForId(staffStationChange.getOldStationId());
                staffStationChange.setOldStationName(oldStationName);
            }
            staffStationChange.setStationName(stationName);
            iStaffService.updateById(staff);
            this.save(staffStationChange);
        }
        return new Result(Code.OK,"","成功");
    }

    @Override
    @Transactional
    public Result staffStationLeave(PersonManage personManage){
        for (StaffStationChange staffStationChange:personManage.getStaffStationChangeList()){
            Staff staff = iStaffService.getById(staffStationChange.getStaffId());
            if (staff == null){
                continue;
            }
            //0：待入职，1：员工，2：离职，3：试岗，4：未入职
            staff.setStatus(staffStationChange.getStatus());
            if (staffStationChange.getChangeDate() != null) {
                staff.setLeaveDate(staffStationChange.getChangeDate());//离职时间
            }
            //staff.setSuperiorId(staffStationChange.getSuperiorId());
            //staff.setStationId(staffStationChange.getStationId());
            //String stationName = stationMapper.getStationNameForId(staffStationChange.getStationId());
            //String oldStationName = stationMapper.getStationNameForId(staffStationChange.getOldStationId());
            //staffStationChange.setStationName(stationName);
            //staffStationChange.setOldStationName(oldStationName);
            iStaffService.updateById(staff);
            this.save(staffStationChange);
        }
        return new Result(Code.OK,"","成功");
    }


    @Override
    public String getStationNameForId(Long id){
        return stationMapper.getStationNameForId(id);
    }

    @Override
    public IPage<StaffStationChange> getTransferList(IPage<?> page, Wrapper wrapper){
        return staffStationChangeMapper.getTransferList(page,wrapper);
    }
}
